package com.wlj.utils.suanfa;

import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.List;

public class Combinations {
    //从data中选取2个数的所有组合
    public void combinations(List<Integer> selected,
                            List<Integer> data, int n){
        if(n==0){ //已经选光
            for(Integer i:selected){
                System.out.print(i);
                System.out.print(" ");
            }
            System.out.println();
            return;
        }

        if(data.isEmpty()){ //考虑特殊情况:一个元素都没有
            return;
        }
        //1.选取第一个元素
        selected.add(data.get(0));
        combinations(selected,data.subList(1,data.size()),n-1);

        //2.不选取第一个元素
        selected.remove(data.get(0));
        combinations(selected,data.subList(1,data.size()),n);

    }

    public static void main(String[] args){
        Combinations combinations = new Combinations();
        combinations.combinations(new ArrayList<>(), Lists.newArrayList(1,2,3,4,5),4);
    }
}
